package com.pmcc.utils;

import java.util.Random;

/**
 * 生成随机字符串
 */
public class RandomUtils {

    private static String NUM="num";// 随机数
    private static String NUM_LOWER_CASE="numLowerCase";// 随机数与小写
    private static String NUM_UPPER_CASE="numUpperCase";// 随机数与大写
    private static String NUM_STR="numStr";// 随机数与字符
    private static String STR="str";// 随机字符串
    private static String UPPER_CASE="upperCase";// 随机小写字符串
    private static String LOWER_CASE="lowerCase";// 随机大写字符串


    /**
     * 生成随机数 包含大小写字母与数字
     * n ： 需要的长度
     *
     * @return
     */
    public static String createRandomCharAndNum(int length, String type) {
        StringBuilder randomStr = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            if (type.equalsIgnoreCase(RandomUtils.NUM)) {
                randomStr = randomStr.append(random.nextInt(10));
                continue;
            }

            if (type.equalsIgnoreCase(RandomUtils.STR)) {
                int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
                randomStr = randomStr.append((char) (nextInt + random.nextInt(26)));
                continue;
            }

            if (type.equalsIgnoreCase(RandomUtils.UPPER_CASE)) {
                randomStr = randomStr.append((char) (97 + random.nextInt(26)));
                continue;
            }

            if (type.equalsIgnoreCase(RandomUtils.LOWER_CASE)) {
                randomStr = randomStr.append((char) (65 + random.nextInt(26)));
                continue;
            }

            String str = random.nextInt(2) % 2 == 0 ? "num" : "char";
            if (type.equalsIgnoreCase(RandomUtils.NUM_LOWER_CASE)) {
                if ("char".equalsIgnoreCase(str)) { // 产生字母
                    // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                    randomStr = randomStr.append((char) (97 + random.nextInt(26)));
                } else { // 产生数字
                    randomStr = randomStr.append(random.nextInt(10));
                }
                continue;
            }
            if (type.equalsIgnoreCase(RandomUtils.NUM_UPPER_CASE)) {
                if ("char".equalsIgnoreCase(str)) { // 产生字母
                    // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                    randomStr = randomStr.append((char) (65 + random.nextInt(26)));
                } else { // 产生数字
                    randomStr = randomStr.append(random.nextInt(10));
                }
                continue;
            }
            if (type.equalsIgnoreCase(RandomUtils.NUM_STR)) {
                if ("char".equalsIgnoreCase(str)) { // 产生字母
                    int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
                    // System.out.println(nextInt + "!!!!"); 1,0,1,1,1,0,0
                    randomStr = randomStr.append((char) (nextInt + random.nextInt(26)));
                } else { // 产生数字
                    randomStr = randomStr.append(random.nextInt(10));
                }
            }
        }
        return randomStr.toString();
    }

    /**
     * 获取随机数字
     *
     * @param num 获取几位数字
     * @return
     */
    public static String createRandom(int num) {
        String randomNum = "";
        for (int i = 0; i < num; i++) {
            randomNum = randomNum + (int) (Math.random() * 9);
        }
        return randomNum;
    }


    public static void main(String[] args) {
        System.out.println(RandomUtils.NUM+"------"+RandomUtils.createRandomCharAndNum(16, RandomUtils.NUM));
        System.out.println(RandomUtils.NUM_LOWER_CASE+"------"+RandomUtils.createRandomCharAndNum(16, RandomUtils.NUM_LOWER_CASE));
        System.out.println(RandomUtils.NUM_UPPER_CASE+"------"+RandomUtils.createRandomCharAndNum(16, RandomUtils.NUM_UPPER_CASE));
        System.out.println(RandomUtils.NUM_STR+"------"+RandomUtils.createRandomCharAndNum(16, RandomUtils.NUM_STR));
        System.out.println(RandomUtils.STR+"------"+RandomUtils.createRandomCharAndNum(16, RandomUtils.STR));
        System.out.println(RandomUtils.UPPER_CASE+"------"+RandomUtils.createRandomCharAndNum(16, RandomUtils.UPPER_CASE));
        System.out.println(RandomUtils.LOWER_CASE+"------"+RandomUtils.createRandomCharAndNum(16, RandomUtils.LOWER_CASE));
    }
}
